package com.lp.business.dao;

import java.util.List;

import org.iframework.commons.domain.order.Order;
import org.iframework.commons.domain.pager.Pager;
import org.iframework.commons.util.fast.D;
import org.iframework.commons.util.fast.V;
import org.iframework.support.spring.hibernate.dao.BaseHibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.lp.business.model.User;

/**
 * 用户数据访问实现类
 * 
 * @author lp
 * 
 */
@Repository("userDao")
public class UserDao extends BaseHibernateDaoSupport<User, String> {

	@Override
	public List<User> findByModel(User model, Order order, Pager pager) {
		model = model == null ? new User() : model;
		StringBuilder hql = new StringBuilder();
		hql.append("from User c where 1=1");
		hql.append(V.isNotEmpty(model.getId()) ? " and c.id=:id " : "");
		hql.append(V.isNotEmpty(model.getLoginName()) ? " and c.loginName=:loginName " : "");
		hql.append(V.isNotEmpty(model.getMobile()) ? " and c.mobile=:mobile " : "");
		hql.append(V.isNotEmpty(model.getEmail()) ? " and c.email=:email " : "");
		hql.append(V.isNotEmpty(model.getAccountOpenId()) ? " and c.accountOpenId=:accountOpenId " : "");
		hql.append(V.isNotEmpty(model.getUserName()) ? " and c.userName like '%"+model.getUserName()+"%' " : "");
		
		hql.append(V.isNotEmpty(model.getStatus()) ? " and c.status=:status " : "");
		hql.append(V.isNotEmpty(model.getDateStart()) ? " and c.createTime>'"+D.format(model.getDateStart(), "yyyy-MM-dd HH:mm:ss")+"'" : "");
		hql.append(V.isNotEmpty(model.getDateEnd()) ? " and c.createTime<'"+D.format(model.getDateEnd(), "yyyy-MM-dd HH:mm:ss")+"'" : "");
		
		hql.append(order != null ? order.toString() : "");
		return this.findByQueryString(hql.toString(),model, pager);
	}
	
	
	public User findUserByLoginNameAndLoginPwd(String loginName, String loginPwd) {
		List<User> users = this.queryHQL("from User a where a.status='AVAILABLE' and a.loginName='" + loginName + "' and a.loginPwd='" + loginPwd+ "'");
		return users!=null&&users.size()>0?users.get(0):null;
	}
	
	
	public boolean isExistsLoginName(String loginName) {
		List<User> users = this.queryHQL("from User a where a.status='AVAILABLE' and a.loginName='" + loginName + "'");
		return users!=null&&users.size()>0?true:false;
	}
	
	
	public User findUserByMoible(String mobile){
		List<User> users = this.queryHQL("from User a where a.status='AVAILABLE' and a.mobile='" + mobile + "'");
		return users != null && users.size() > 0 ? users.get(0) : null;
	}
	
}
